package net.zjitc.servlet.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Administrator
 * @Date 2020/5/14 15:42
 * 请求request：
 *   1）获取请求消息
 *      HTTP请求格式： 请求行、请求头、空行、请求体（post才有）
 **/
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /* req： 请求对象request； resp：响应对象 response*/
        /* 1、获取请求行消息：(GET /login.html  HTTP/1.1) */
        /*    获取项目虚拟路径：getContextPath()
              获取Servlet路径（别名）：getServletPath()
              URL（统一资源定位符），完整的访问路径：getRequestURL()
              URI（项目虚拟路径+servlet路径）：getRequestURI()
              */
        String contextPath = req.getContextPath();
        String servletPath = req.getServletPath();
        StringBuffer requestURL = req.getRequestURL();
        String requestURI = req.getRequestURI();
        System.out.println(contextPath);
        System.out.println(servletPath);
        System.out.println(requestURL);
        System.out.println(requestURI);

        String method = req.getMethod();
        System.out.println(method);

        /* 2、获取请求头数据：*/
        String agent = req.getHeader("User-Agent");
        System.out.println(agent);
        /* 判断是哪个浏览器访问*/
        if(agent.contains("Chrome")){
            System.out.println("谷歌浏览器");
        }else if(agent.contains("Firefox")){
            System.out.println("火狐浏览器");
        }else{
            System.out.println("其他浏览器");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        不用管是get还是post请求
        this.doGet(req,resp);
    }
}
